package me.cxlr.qinlauncher2.view.launcher;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextClock;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import me.cxlr.qinlauncher2.R;
import me.cxlr.qinlauncher2.util.ThemeUtil;
import me.cxlr.qinlauncher2.view.LauncherActivity;

/**
 * @author 酷安用户:山轮纸
 */
public class LauncherFragment extends Fragment {
    private View root;
    private LinearLayoutCompat llDesktop;
    private LinearLayoutCompat llDock;
    private LinearLayoutCompat llFunctionKey;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // View
        root = inflater.inflate(R.layout.fragment_launcher, container, false);

        this.initDesktop();
        this.initDock();
        this.initVirtualFunctionKey();

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();

        ThemeUtil.initTheme();
        this.loadDesktop();
        this.loadDock();
        this.loadVirtualFunctionKey();
    }

    /*** 时钟和日期 */
    private TextClock timeClock;
    private TextClock dateClock;

    /**
     * 初始化桌面
     */
    private void initDesktop() {
        llDesktop = root.findViewById(R.id.launcher_desktop);
        timeClock = root.findViewById(R.id.launcher_tc_time);
        dateClock = root.findViewById(R.id.launcher_tc_date);
    }

    /**
     * 加载桌面
     */
    private void loadDesktop() {
        Desktop.getInstance().loadClock(timeClock, dateClock);
    }

    /*** 桌面托盘 */
    private CardView cvDock;
    private LinearLayoutCompat llDock1;
    private LinearLayoutCompat llDock2;
    private LinearLayoutCompat llDock3;
    private LinearLayoutCompat llDock4;
    private LinearLayoutCompat llDock5;
    private LinearLayoutCompat llDockIcon1;
    private LinearLayoutCompat llDockIcon2;
    private LinearLayoutCompat llDockIcon3;
    private LinearLayoutCompat llDockIcon4;
    private LinearLayoutCompat llDockIcon5;
    private AppCompatImageView imgDock1;
    private AppCompatImageView imgDock2;
    private AppCompatImageView imgDock3;
    private AppCompatImageView imgDock4;
    private AppCompatImageView imgDock5;

    /**
     * 加载桌面托盘
     */
    private void initDock() {
        llDock = root.findViewById(R.id.launcher_dock);
        cvDock = root.findViewById(R.id.launcher_cv_dock);
        llDock1 = root.findViewById(R.id.dock_ll_1);
        llDock2 = root.findViewById(R.id.dock_ll_2);
        llDock3 = root.findViewById(R.id.dock_ll_3);
        llDock4 = root.findViewById(R.id.dock_ll_4);
        llDock5 = root.findViewById(R.id.dock_ll_5);
        llDockIcon1 = root.findViewById(R.id.dock_ll_icon_1);
        llDockIcon2 = root.findViewById(R.id.dock_ll_icon_2);
        llDockIcon3 = root.findViewById(R.id.dock_ll_icon_3);
        llDockIcon4 = root.findViewById(R.id.dock_ll_icon_4);
        llDockIcon5 = root.findViewById(R.id.dock_ll_icon_5);
        imgDock1 = root.findViewById(R.id.dock_icon_1);
        imgDock2 = root.findViewById(R.id.dock_icon_2);
        imgDock3 = root.findViewById(R.id.dock_icon_3);
        imgDock4 = root.findViewById(R.id.dock_icon_4);
        imgDock5 = root.findViewById(R.id.dock_icon_5);
    }

    /**
     * 加载桌面托盘
     */
    private void loadDock() {
        Dock.getInstance().loadDock(llDock1, llDock2, llDock3, llDock4, llDock5,
                llDockIcon1, llDockIcon2, llDockIcon3, llDockIcon4, llDockIcon5,
                imgDock1, imgDock2, imgDock3, imgDock4, imgDock5,
                cvDock, requireActivity(), requireContext());
    }

    /*** 左屏幕虚拟按键 */
    private AppCompatTextView tvLeftVirtualFunctionKey;
    /*** 右屏幕虚拟按键 */
    private AppCompatTextView tvRightVirtualFunctionKey;

    /**
     * init左右屏幕虚拟功能键
     */
    private void initVirtualFunctionKey() {
        llFunctionKey = root.findViewById(R.id.launcher_function_key);
        tvLeftVirtualFunctionKey = root.findViewById(R.id.launcher_btn_left_function_key);
        tvRightVirtualFunctionKey = root.findViewById(R.id.launcher_btn_right_function_key);
    }

    /**
     * 加载左右屏幕虚拟功能键
     */
    private void loadVirtualFunctionKey() {
        FunctionKey.getInstance().leftVirtualFunctionKey(requireActivity(), tvLeftVirtualFunctionKey);
        FunctionKey.getInstance().rightVirtualFunctionKey(requireActivity(), tvRightVirtualFunctionKey);
    }
}
